# 已参考资料：
#
# - [Making tmux Pretty and Usable](https://www.hamvocke.com/blog/a-guide-to-customizing-your-tmux-conf/)
# - [tmux 打造精美终端](https://juejin.im/entry/6844903461713281031)

# 修改本文后，运行`tmux source ~/.tmux.conf`才能重新生效

# 创建一个 session 并删除它，目的是为了让 session 的 base index 是 1
# 以下代码不奏效
# if-shell -b 'tmux ls' '' 'new kill-session -t0'

# remap prefix from 'C-b' to 'M-a'
unbind C-b
set-option -g prefix M-a
bind-key M-a send-prefix

# split panes and windows
bind - split-window -v -c "#{pane_current_path}"
bind | split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"
unbind '"'
unbind %

# reload and edit config file
bind r source-file ~/.tmux.conf\; display "Config reloaded!"
bind e neww -n tmux-config "\${EDITOR:-vim} ~/.tmux.conf"

# switch sessions
bind ( switch-client -p
bind ) switch-client -n

# switch panes using Alt-arrow without prefix
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# moving between panes with Prefix h,j,k,l 
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# resize panes
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Quick window selection 
bind -r N select-window -t :+
bind -r P select-window -t :-

# Clear the pane
bind C-l send-keys -R

# copy-mode 将快捷键设置为 vi 模式
set-window-option -g mode-keys vi
# 禁止复制后退出
unbind -T copy-mode-vi MouseDragEnd1Pane
# 定义灵活的复制粘贴映射，支持系统剪切版。需要预先安装 xclip。
# `PREFIX [`进入 copy mode，`PREFIX ]`粘贴最近的 buffer.
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
# 切换 copy stay，需要将 toggle-copy-stay.sh 复制到 ~/.tmux/scripts 目录下
bind -T copy-mode-vi t run-shell -b '~/.tmux/scripts/toggle-copy-stay.sh'

# Enable mouse mode (tmux 2.1 and above)
set-option -g mouse on

# 让鼠标可以将选中的内容复制到剪贴板
# > https://unix.stackexchange.com/a/318285
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M
bind -n C-WheelUpPane select-pane -t= \; copy-mode -e \; send-keys -M
bind -T copy-mode-vi    C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-vi    C-WheelDownPane send-keys -X halfpage-down
bind -T copy-mode-emacs C-WheelUpPane   send-keys -X halfpage-up
bind -T copy-mode-emacs C-WheelDownPane send-keys -X halfpage-down

# 以下命令让鼠标选中即复制。由于复制后会立刻退出 copy mode 模式，不是很实用
# unbind -T copy-mode-vi Enter
# bind-key -T copy-mode-vi Enter send-keys -X copy-pipe-and-cancel "xclip -selection c"
# bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"

# 让 vim 等软件的 esc 延迟为 0
set-option -sg escape-time 0
# 让 vim 等软件的着色显示正常
set-option -g default-terminal "screen-256color" # or "xterm-256color"

# 设置 window 的 base index 为 1
set-option -g base-index 1
# 设置 pane 的 base index 为 1
set-window-option -g pane-base-index 1

set-option -g repeat-time 1000

### 设置样式

set-option -g default-terminal "screen-256color"

# 设置主题，需要提前克隆 GitHub 仓库 edouard-lopez/tmux-tomorrow
source-file ~/.tmux/themes/tomorrow/tomorrow-night-eighties.tmux

# 设置状态栏的背景色
set-option -g status-style "bg=#00346e,fg=#ffffd7"   # tomorrow night blue, base3

set-window-option -g window-style 'fg=white,bg=#393939'
set-window-option -g window-active-style 'fg=white,bg=#2d2d2d'
set-window-option -g window-status-current-format '#[fg=#f99157,bold,bg=#2d2d2d] #I #[fg=#cccccc,bold,bg=#393939] #W^#P #[default]'
set-window-option -g status-justify centre

# 延长消息显示时长
set-option -g display-time 5000

# enable activity alerts 
# set-window-option -g monitor-activity off
# set-option -g visual-activity off

# List of plugins
set-option -g @plugin 'tmux-plugins/tpm'
set-option -g @plugin 'ChanderG/tmux-notify'
set-option -g @plugin 'run27017/tmux-vim-exit'
# Other examples:
# set-option -g @plugin 'github_username/plugin_name'
# set-option -g @plugin 'git@github.com/user/plugin'
# set-option -g @plugin 'git@bitbucket.com/user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
# 初始化步骤：
# 1. `git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm`
# 2. `prefix + I`
run '~/.tmux/plugins/tpm/tpm' 

